package generic;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class CustomArrayStack<T> {

    private Object[] arr;
    private int top;

    public CustomArrayStack(int capacity) {
        arr = new Object[capacity];
        top = -1;
    }

    // 入栈
    public void push(T value) {
        if (top == arr.length - 1) {
            throw new RuntimeException("栈已满，无法入栈");
        }
        arr[++top] = value;
    }

    // 出栈
    public T pop() {
        if (top == -1) {
            throw new RuntimeException("栈为空，无法出栈");
        }
        return (T) arr[top--];
    }

    // 查看栈顶元素
    public  T peek() {
        if (top == -1) {
            throw new RuntimeException("栈为空，无法查看栈顶元素");
        }
        return (T) arr[top];
    }

    public static  <E> E getRandomElement(List<E> list) {
        Random random = new Random();
        return list.get(random.nextInt(list.size()));
    }

    public static void main(String[] args) {
//        CustomArrayStack<String> stringStack = new CustomArrayStack<>(2);
//        stringStack.push("guslegend");
//        stringStack.push("springboot");
//        String stringValue = stringStack.pop();
//        System.out.println(stringValue);
//
//        CustomArrayStack<Integer> integerStack = new CustomArrayStack<>(2);
//        integerStack.push(12);
//        integerStack.push(88);
//        Integer integerValue = integerStack.pop();
//        System.out.println(integerValue);
//
//        System.out.println(stringStack.getClass());
//        System.out.println(integerStack.getClass());

        List<String> list = Arrays.asList("springBoot", "Activiti", "LangChain");
        CustomArrayStack<String> stringStack = new CustomArrayStack<>(2);
        String randomElement = stringStack.getRandomElement(list);
        System.out.println(randomElement);
    }
}